package cn.disicn.util;

import java.util.HashMap;
import java.util.Set;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.jfinal.kit.PropKit;

public class SMSUtil {

	/*
	 * 选择环境 1.沙箱环境 2.生产环境
	 */
	private static String init(Integer init) {
		// 返回的环境
		String str = "";

		if (init == 1) {
			str = "sandboxapp.cloopen.com";
		} else if (init == 2) {
			str = "app.cloopen.com";
		}
		return str;
	}

	/*
	 * 生成6位数验证码
	 */
	private static String yzm() {
		Integer yzm = (int) ((Math.random() * 9 + 1) * 100000);
		return yzm.toString();
	}

	/*
	 * 短信验证
	 */
	public static String getphone(String phone) {
		// 返回的字符串
		String str = "";
		// 生成随机6位数的验证码
		String yzm = yzm();
		HashMap<String, Object> result = null;
		// 初始化SDK
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(init(1), PropKit.use("sms.properties").get("post"));
		restAPI.setAccount(PropKit.use("sms.properties").get("sId"), PropKit.use("sms.properties").get("token"));
		restAPI.setAppId(PropKit.use("sms.properties").get("appid"));
		result = restAPI.sendTemplateSMS(phone, PropKit.use("sms.properties").get("mbid"),
				new String[] { yzm, PropKit.use("sms.properties").get("two") + "分钟" });
		if ("000000".equals(result.get("statusCode"))) {
			// 正常返回输出data包体信息（map）
			HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for (String key : keySet) {
				Object object = data.get(key);
				System.out.println(key + " = " + object);
			}
			str = yzm;
		} else {
			// 异常返回输出错误码和错误信息
			System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
			str = "error";
		}
		return str;
	}
}
